package edu.ntu.im.p2ptrade.entity;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import edu.ntu.im.p2ptrade.P2PTrade;

public class DialogRepository {
	private static DialogRepository me;
	private HashMap<String, List<Dialog>> dialogs = new HashMap<String, List<Dialog>>();
	private Date lastUpdate = new Date();
	public static DialogRepository getInstance() {
		if (me == null){
			me = new DialogRepository();
		}
		return me;
	}
	private  DialogRepository(){
	}
	public void addDialog(Dialog dialog) {
		String key = null;
		if (!P2PTrade.getInstance().getPeerName().equals(dialog.getFrom())){
			key = dialog.getFrom();
		}else {
			key = dialog.getTo();
		}
		if (this.dialogs.get(key)==null){
			this.dialogs.put(key, new LinkedList<Dialog>());
		}
		this.dialogs.get(key).add(dialog);
		System.out.println(P2PTrade.getInstance().getPeerName()+" have dialog with "+key+":"+this.getDialogWith(key).toString());
		this.lastUpdate = new Date();
	}
	
	public List<Dialog> getDialogWith(String peer){
		return this.dialogs.get(peer);
	}
	
	public HashMap<String, List<Dialog>> getDialogs(){
		return this.dialogs;
	}
	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lasUpdate) {
		this.lastUpdate = lasUpdate;
	}

}
